<?php


/**
 * 汇总统计管理
 * Enter description here ...
 * @author Admin
 *
 */
class CollectAction extends  CommonAction{
	
	
	/**
	 * 商品竞拍情况汇总统计列表
	 * Enter description here ...
	 */
	public function auctionStateList(){
		Log::write("...商品竞拍情况汇总统计列表 start...",Log::DEBUG);
		
		$auction = D('Auction');   //竞拍表
		$auction_log = D('Auction_log'); //竞拍商品表
		
		$user = D('User');
		
		$config = C('SYSCONFIG');
		
		$prefix = C('DB_PREFIX');
		
		$goodsname =  $_REQUEST['goodsname'];      //检索条件
		$this->assign("goodsname",$goodsname);	
		
		
		$where['state'] = array('egt',2);  //竞拍结束
		$where['goodsname']= array('like',"%{$goodsname}%"); 
		
		$count = $auction->where($where)->order('endtime desc')->count(); 
		

		import("@.ORG.Page"); //导入分页类
		
		$p = new Page ($count,12);   //分页构造方法
		
		$datalist = $auction->where($where)->order('endtime desc')
					->limit($p->firstRow.','.$p->listRows)-> findAll();
		
					  
		//....竞拍表中存在进货价、零售价、最后竞拍人、竞拍金额、机器人竞拍金额			

		foreach($datalist as &$value){  //修改数组引用
			
			//Log::write("...最近结束竞拍信息..".$key .'    :' .$value,Log::DEBUG);
			$wh['aid']=array('eq',$value['id']);
			$bidlog = $auction_log->where($wh)->count();    //获取商品竞拍日志
			
			if($bidlog){  //true =1  false =0
				
				unset($rsu);
				$rsu = $this->getAuctionLog($value['id'],0);
				
				$value['beatnum'] = $rsu[0]['beatnum'];
				$value['pointbnum'] = $rsu[0]['pointbnum'];
				
				/* 机器人竞标情况
				unset($rsu1);
				$rsu1 = $this->getAuctionLog($value['id'],1);
				
				$value['robotbeatnum'] = $rsu1[0]['beatnum'];
				$value['robotpointbnum'] = $rsu1[0]['pointbnum'];
				*/
				
				$retprice =  $rsu[0]['beatnum'] /$config['rmb_beat']  ;  //用户实体币折算金额
				$value['retprice'] = $retprice;   //折算用户实体币金额
				
				
				if(($retprice+$value['realprice']) > $value['stockprice'] ){   //折算金额 + 竞标金额 > 成本价
					$value['retstate'] = 1 ;
				}	else{   //亏损
					$value['retstate'] = 0 ;
				}
			
				
				$urs = $user->find($value['uid']); 
				if($urs['robot'] == 1){
					$value['robotstate'] = '机器人';
				}else{
					$value['robotstate'] = '注册用户';
				}
				
				
				
				
				
			}
				
		}
		
		
		
		
		//dump($datalist);
		
					  
		$page = $p->show();   
		
		$this->assign("page",$page);
		$this->assign("datalist",$datalist);
		
		$this->display();
		
		Log::write("...商品竞拍情况汇总统计列表 end...",Log::DEBUG);
	}
	
	
	
	
	/**
	 * 获取竞拍历史情况
	 * Enter description here ...
	 */
	private function getAuctionLog($auctionid,$state){
		//Log::write("...获取竞拍历史情况statrt...:"  .$auctionid   .'    :' .$state  ,Log::DEBUG);
		$auction = D('Auction');
		//....统计竞标情况：注册用户或机器人竞拍所用实体币、虚拟币数
		$prefix = C('DB_PREFIX');
		
		$sql = "select  sum(t.`beat`) beatnum,sum(t.`pointb`) pointbnum  from {$prefix}auction_log t  
				 inner join cms_user u where t.`uid` = u.id  and t.`aid`={$auctionid} and u.`robot`={$state}
			" ;
		//Log::write("...获取竞拍历史情况sql" . $sql ,Log::DEBUG);
		
		$rs = $auction->query($sql) ;
		
		return $rs;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	/**
	 * 商品竞标情况统计
	 * Enter description here ...
	 */
	public function goodsAuctionList(){
		
		//Log::write("...商品竞标情况统计start"  ,Log::DEBUG);
		
		$goods = D('Goods');  //商品
		$auction = D('Auction');  //商品竞标
		
		
		$goodsname =  $_REQUEST['goodsname'];      //检索条件
		$this->assign("goodsname",$goodsname);	
		
		$where['goodsname']= array('like',"%{$goodsname}%"); 
		
		import("@.ORG.Page"); //导入分页类
		
		$count = $goods->where($where)->count() ; //总数
		$p = new Page ($count,12);   //分页构造方法
		
		$goodsList = $goods->order('edittime desc')
					->where($where)
					->limit($p->firstRow.','.$p->listRows)
					-> findAll();
					
					
		foreach($goodsList as &$value){  //数组引用
			
			unset($aucount);
			//......统计商品竞标情况
			$wh['gid'] = array('eq',$value['id']);
			$aucount = $auction->where($wh)->count();  //商品竞标统计
			
			$value['aucount'] = $aucount ;
			
			//...统计竞标参与人数
			unset($rbotrs);
			$rbotrs = $this->getparticipateuser($value['id'],1);   //机器人参与
			
			$value['rbotcount'] = $rbotrs[0]['cumt'] ;
			
			unset($rs);
			$rs = $this->getparticipateuser($value['id'],0);  //注册用户参与
			
			$value['userscount'] = $rs[0]['cumt'] ;
		}			
		
		
		$this->assign("goodsList",$goodsList);
		
		//Log::write("...商品竞标情况统计end"  ,Log::DEBUG);
		
		$this->display();
	}
	
	
	
	/**
	 * 获取商品竞标人数
	 * Enter description here ...
	 * @param unknown_type $goodid
	 * @param unknown_type $robot
	 */
	private function getparticipateuser($goodid,$robot){
		$part = M('participate');
		
		$sql = "SELECT count(*) cumt FROM cms_participate c, cms_user pu 
				WHERE  EXISTS  (SELECT 1  FROM cms_auction u  WHERE u.gid = $goodid AND u.id = c.aid)
				 AND c.uid = pu.id  AND pu.robot = $robot
			" ;
		
		//Log::write("...fffffff获取商品竞标人数SQLfffffff:" .$sql  ,Log::DEBUG);
			
		$rs = $part->query($sql);
		
		
		return $rs ; 
		
	}
	
	
	
	
  
 
             
                
               
      
      
	
	
	
	
	
}